package mythical.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dto.MethodDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import mythical.exceptions.DataBaseException;
import mythical.mapper.MethodInsideMapper;
import mythical.mapper.MethodMapper;
import mythical.mapper.MethodSkillMapper;
import mythical.service.MethodService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import po.Method;
import po.MethodInside;
import po.MethodSkill;
import vo.MethodVo;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class MethodServiceimpl extends ServiceImpl<MethodMapper, Method> implements MethodService {

    private final MethodSkillMapper methodSkillMapper;

    private final MethodMapper methodMapper;

    private final MethodInsideMapper methodInsideMapper;

    /**
     * 用户获得功法
     * @param methodDto
     */
    @Override
    @Transactional
    public void isave(MethodDto methodDto) {
        Method method = new Method();
        BeanUtil.copyProperties(methodDto,method);
        Long id = StpUtil.getLoginIdAsLong();
        method.setUserId(id);
        try {
            save(method);
        } catch (Exception e) {
            throw new DataBaseException("已存在该功法！");
        }
        //查询是否携带技能
        List<MethodSkill> methodSkillList = methodDto.getMethodSkillList();
        if (CollUtil.isNotEmpty(methodSkillList)){
            methodSkillList.forEach(skill -> {
                skill.setMethodId(method.getId());
                methodSkillMapper.insert(skill);
            });
        }
        //查询是否为内功
        List<MethodInside> methodInsideList = methodDto.getMethodInsideList();
        if (CollUtil.isNotEmpty(methodInsideList)) {
            methodInsideList.forEach(methodInside -> {
                methodInside.setMethodId(method.getId());
                methodInsideMapper.insert(methodInside);
            });
        }

    }

    @Override
    @Transactional
    public List<MethodVo> ilist() {
        //构建查询条件查询出用户所拥有的功法
        LambdaQueryWrapper<Method> wrapper = new LambdaQueryWrapper<Method>()
                .ge(Method::getUserId,StpUtil.getLoginIdAsLong());
        List<Method> methods = methodMapper.selectList(wrapper);
        List<MethodVo> methodVoList = new ArrayList<>();
        methods.forEach(method -> {
            MethodVo methodVo = new MethodVo();
            BeanUtil.copyProperties(method,methodVo);
            //构建查询条件将每套功法的对应的技能放入
            LambdaQueryWrapper<MethodSkill> skillLambdaQueryWrapper = new LambdaQueryWrapper<MethodSkill>()
                    .eq(MethodSkill::getMethodId, method.getId());
            List<MethodSkill> methodSkills = methodSkillMapper.selectList(skillLambdaQueryWrapper);
            if (CollUtil.isNotEmpty(methodSkills)) {
                methodVo.setMethodSkillList(methodSkills);
            }
            //构建查询条件将每套功法的内力加成放入
            LambdaQueryWrapper<MethodInside> methodInsideLambdaQueryWrapper = new LambdaQueryWrapper<MethodInside>()
                    .eq(MethodInside::getMethodId, method.getId());
            List<MethodInside> methodInsideList = methodInsideMapper.selectList(methodInsideLambdaQueryWrapper);
            if (CollUtil.isNotEmpty(methodInsideList)) {
                methodVo.setMethodInsideList(methodInsideList);
            }
            methodVoList.add(methodVo);
        });

        return methodVoList;
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        ids.forEach(id -> {
            //删除功法对应的技能
            LambdaQueryWrapper<MethodSkill> skillLambdaQueryWrapper = new LambdaQueryWrapper<MethodSkill>()
                    .eq(MethodSkill::getMethodId, id);
            methodSkillMapper.delete(skillLambdaQueryWrapper);
            //删除功法对应的内力
            LambdaQueryWrapper<MethodInside> insideLambdaQueryWrapper = new LambdaQueryWrapper<MethodInside>()
                    .eq(MethodInside::getMethodId, id);
            methodInsideMapper.delete(insideLambdaQueryWrapper);
            removeById(id);
        });
    }

}
